<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<style>
    ul,
    li,
    ol,
    dl,
    dt,
    dd,
    div,
    p,
    span,
    h1,
    h2,
    h3,
    h4,
    h5,
    h6,
    a {
        padding: 0px;
        margin: 0px;
    }


    a {
        text-decoration: none;
        color: #333333;
    }

    .login-header {
        width: 100%;
        margin: 0 auto;

        height: 30px;
        font-size: 24px;
        line-height: 30px;
        text-align: center;
    }

    .login {
        display: none;

        /* 子绝父相 */
        position: fixed;
        left: 50%;
        top: 50%;
        z-index: 9999;
        transform: translate(-50%, -50%);

        width: 512px;
        height: 280px;

        background-color: #fff;
        box-shadow: 0px 0px 20px #ddd;
        border: 1px solid #ebebeb;
        border-radius: 5px;
    }

    .login-title {
        position: relative;
        width: 100%;
        height: 40px;
        margin: 10px 0 0 0;

        line-height: 40px;
        font-size: 18px;

        cursor: move;
        text-align: center;
    }

    .login-input-content {
        margin-top: 20px;
    }


    .login-input input.list-input {
        float: left;
        height: 35px;
        width: 350px;

        border: 1px solid #ebebeb;
        line-height: 35px;
        /* 文本缩进, 控制输入框中 默认文字的缩进 */
        text-indent: 5px;
    }

    .login-input {
        /* 这行代码是为了什么 */
        overflow: hidden;
        margin: 0px 0px 20px 0px;
    }

    .login-input label {
        float: left;
        width: 90px;
        height: 35px;
        padding-right: 10px;

        font-size: 14px;
        text-align: right;
        line-height: 35px;
    }

    .login-title span {
        position: absolute;
        font-size: 12px;
        right: -20px;
        top: -30px;
        background: #ffffff;
        border: #ebebeb solid 1px;
        width: 40px;
        height: 40px;
        border-radius: 20px;
    }

    .login-button {
        width: 50%;
        margin: 30px auto 0px auto;


        line-height: 40px;
        font-size: 14px;
        border: 1px solid #ebebeb;
        text-align: center;
    }

    .login-button a {
        display: block;
    }

    .login-bg {
        display: none;
        width: 100%;
        height: 100%;
        position: fixed;
        top: 0px;
        left: 0px;
        background: rgba(0, 0, 0, .3);
    }
</style>
<body>

<div class="login-header">
    <a href="javascript:;">点击, 弹出登录框</a>
</div>

<div class="login" id="login">
    <div class="login-title" id="login-title">登录会员
        <span><a class="close-login" href="javascript:;" id="closeBtn">关闭</a></span>
    </div>
    <div class="login-input-content">
        <div class="login-input">
            <label for="username">用户名: </label>
            <input class="list-input" id="username" name="info[username]" placeholder="请输入用户名" type="text">
        </div>
        <div class="login-input">
            <label for="password">密码: </label>
            <input class="list-input" id="password" name="info[password]" placeholder="请输入登录密码" type="password">
        </div>
    </div>
    <div class="login-button" id="loginBtn"><a href="javascript:;" id="login-button-submit">登录会员</a></div>
</div>

<div class="login-bg" id="bg"></div>

<script>

    /**
     * 需求:
     * 1.点击弹出层, 会弹出模态框, 并且显示灰色半透明的遮挡层
     * 2.点击关闭按钮, 可以关闭模态框, 并且同时关闭灰色半透明遮挡层
     * 3.鼠标放到模态框最上面一行, 可以按住鼠标拖拽模态框在页面中移动
     * 4.鼠标松开, 可以停止拖动模态框移动
     */

    var loginHeader = document.querySelector('.login-header');
    var loginTitle = document.querySelector('.login-title');
    var closeLogin = document.querySelector('.close-login');
    var login = document.querySelector('#login');
    var mask = document.querySelector('#bg');

    // 2.点击弹出层这个 login和 mask 都显示出现
    loginHeader.addEventListener('click', function () {
        mask.style.display = 'block';
        login.style.display = 'block';
    })

    closeLogin.addEventListener('click', function () {
        mask.style.display = 'none';
        login.style.display = 'none';
    })

    // 4.拖拽
    // (1) 当我们鼠标按下(mousedown), 就获取的鼠标在盒子内坐标
    loginTitle.addEventListener('mousedown', function (e) {
        // 鼠标在盒子内坐标
        var x = e.pageX - login.offsetLeft;
        var y = e.pageY - login.offsetTop;

        // (2) 鼠标移动的时候, 把鼠标在页面的坐标, 减去鼠标在盒子内的坐标就是模态框的left和top值
        document.addEventListener('mousemove', move)

        function move(evt) {
            login.style.left = evt.pageX - x + 'px';
            login.style.top = evt.pageY - y + 'px';
        }

        // (3) 鼠标弹起, 就让鼠标移动事件解除
        document.addEventListener('mouseup', function () {
            document.removeEventListener('mousemove', move);
        })
    })

</script>
</body>

</html>
